import eControl from '@/components/e-control'
import eTable from '@/components/e-table'
import tableConfig from '@/mixins/defaultTablePage.mixin'
import { downBlobFile } from '/@/utils/other';
import { ElMessage } from 'element-plus';

export default {
  name: "DefaultTableList",
  components: {
    eControl,
    eTable,
    // eForm,
  },
  mixins: [tableConfig],
  props: {
    type: {
      type: String,
      default: ''
    },
    rowKey: {
      type: String,
      default: 'id'
    },
    // 如果父组件没传入 loadMethod 方法，则使用该值作为表格数据
    rowData: {
      type: Array,
      default: () => []
    },
    showSearch:{
      type: Boolean,
      default: true
    },
    controlEvents: {
      type: Object,
      default: () => {}
    },
    formOptions: {
      type: Array,
      default: () => []
    },
    btns: {
      type: Array,
      default: () => []
    },
    tableConfig: {
      type: Object,
      default: () => {}
    },
    editFormOptions: {
      type: Object,
      default: () => {}
    },
    // 父页面名称
    superPageName: {
      type: String,
      default: 'default'
    },
    tableHeightOffset: {
      type: Number,
      default: 0
    },
    // 返回请求参数
    beforeLoad: {
      type: Function,
    },
    // 请求方法
    loadMethod: {
      type: Function,
    },
    // 格式化数据
    formatTableData: {
      type: Function,
      default: data => data
    },
  },
  data() {
    return {
      searchLabelWidth: '0px',
      editFormLabelWidth: '130px',
      
      // 编辑框表单数据
      dialogFormCurrent: {},
      expandRows: [],
      // 表格数据
      tableData: [],
      currentSelect: [],
      canEdit: false,
      isSingle: false,
    }
  },
  computed: {
    // 组合查询条件和分页
    searchData() {
      const temp = this.searchFormData
      const { current, size } = this.pageData
      const params = {}
      for (const key in temp) {
        if (key.indexOf('Time') > -1||key.indexOf('Date') > -1) {
          params[`${key}Begin`] = temp[key] && temp[key].length > 0 ? temp[key][0]+' 00:00:00' : '',
          params[`${key}End`] = temp[key] && temp[key].length > 0 ? temp[key][1]+' 23:59:59' : ''
        }
        else {
          params[key] = temp[key]
        }
      }
      return {
        current, size,
        ...params
      }
    },
    tableSlots() {
      return this.tableConfig.mainTable.filter(o => o.slotName && o.slotName.length > 0)
    },
    // 表格最大高度
    tableMaxHeight() {
      return window.innerHeight - this.controlHeight - 175 - this.tableHeightOffset
    },
  },
  mounted() {
    this.init()
  },
  methods: {
    expandChange(row, rows) {
      this.expandRows = rows.map(o => o.drugFinishId)
    },
    /**
     * @functionName getTableData
     * @description 获取表格数据
     * @author 张文韬
     */
    getTableData() {      
      if (!this.loadMethod) {
        const { rowData } = this
        this.tableData = [...rowData]
        this.pageData.total = rowData.length
        return 
      }
      const param = this.beforeLoad? this.beforeLoad(this.searchData, this.type): this.searchData
      this.loadMethod(param).then(async res => {
        this.loading = false
        this.pageData.total = res.data.total
        this.tableData = await this.formatTableData(res.data.records)   
      })
    },
    reload() {
			this.refreshPageCurrent();
		},
    handleSelectAll(e) {
      // const data = this.tableData.filter(o => e.indexOf(o[this.rowKey]))
      this.$emit('select-all', e)
    },
    handleSelectChange(e) {
      // const data = this.tableData.filter(o => e.indexOf(o[this.rowKey]))
      this.$emit('selection-change', e)
    },
    async export(fileName, path) {
			if (this.tableData.length == 0) {
				return ElMessage.warning('表格数据为空');
			}
			try {
        await downBlobFile(
          path,
          this.beforeLoad ? this.beforeLoad(this.searchData, this.type) : this.searchData,
          fileName
        );
      } catch (error) {
        const text = await new Response(error).text();
        console.log('Text:', JSON.parse(text));
        const errRes = JSON.parse(text) || {}
        ElMessage.warning(errRes.msg);
      }
		},
  },
  beforeDestroy() {
    localStorage.removeItem('SETTLEMENT_ORDER_PAGE_CONFIG')
  },
}